// EFECTO SCROLL
var state = false;
var curr = 1;

$(window).load(function(){
    state = true;
});

function slide_up() {
    if(!modal){
        $('.circle').tooltipster('hide');
        if ($('footer').hasClass('active')) {
            $('footer').removeClass('active');
        } else if (curr > 1) {
            --curr;
            $('[class*="block-"]').removeClass('active');
            $('.block-' + curr).addClass('active');
            $('.bullets a').removeClass('active');
            $('.bullets a[data-to='+curr+']').addClass('active');
			$('.slides').attr('class', 'slides has-load curr-' + curr);
        }
    }
}
;

function slide_down() {
    if(!modal){
        $('.circle').tooltipster('hide');
        if (curr < $('.slide').length) {
            ++curr;
            $('[class*="block-"]').removeClass('active');
            $('.block-' + curr).addClass('active');
            $('.bullets a').removeClass('active');
            $('.bullets a[data-to='+curr+']').addClass('active');
			$('.slides').attr('class', 'slides has-load curr-' + curr);
        } else {
            $('footer').addClass('active');
        }
    }
}
;

$(document).on('keydown', function(e) {

    if (state) {
        state = false;
        if (e.which === 38 || e.which === 33) {
            slide_up();
        }
        else if (e.which === 40 || e.which === 34) {
            slide_down();
        }
        setTimeout(function() {
            state = true;
        }, 1000);
    }
});

$(window).on('DOMMouseScroll mousewheel', function(e) {
    if (state) {
        state = false;
        if (e.type == 'mousewheel') {
            if (e.originalEvent.wheelDelta > 0) {
                slide_up();
            }
            else {
                slide_down();
            }
        } else if (e.type == 'DOMMouseScroll') {
            if (e.originalEvent.detail < 0) {
                slide_up();
            }
            else {
                slide_down();
            }
        }
        setTimeout(function() {
            state = true;
        }, 1000);
    }
});


if($(window).width()>640){
    var ts;
    $(document).on('touchstart', function (e){
        if(!$('body').hasClass('modal-on')){
            ts = e.originalEvent.touches[0].clientY;
        }
    });

    $(document).on('touchend', function (e){
        if(!$('body').hasClass('modal-on')){
            var te = e.originalEvent.changedTouches[0].clientY;
            if (state) {
                state = false;
                if(ts > te+5){
                    slide_down();
                }else if(ts < te-5){
                    slide_up();
                }
                setTimeout(function() {
                    state = true;
                }, 800);
            }
        }
    });
    $(document).bind('touchmove', function (e){
        if(!$('body').hasClass('modal-on')){
            e.preventDefault();
        }
    });
}

// BULLETS
$(document).on('click', '[data-to]', function(e){
    e.preventDefault();
    curr = $(this).data('to');
    $('footer').removeClass('active');
    $('[class*="block-"]').removeClass('active');
    $('.block-' + curr).addClass('active');
    $('.bullets a').removeClass('active');
    $('.bullets a[data-to='+curr+']').addClass('active');
	$('.slides').attr('class', 'slides has-load curr-' + curr);
});

// ARROW CLICK
$(document).on('click', '.down', function(e){
    e.preventDefault();
  slide_down();
});

$(document).on('click', '.up', function(e){
    e.preventDefault();
  slide_up();
});
